package cn.zero.letaomallspringboot.service.impl;

import cn.zero.letaomallspringboot.common.PageResult;
import cn.zero.letaomallspringboot.entity.RefundRecord;
import cn.zero.letaomallspringboot.mapper.RefundRecordMapper;
import cn.zero.letaomallspringboot.service.IRefundRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RefundRecordService implements IRefundRecordService {

    private final RefundRecordMapper refundRecordMapper;

    private static final Logger logger = LoggerFactory.getLogger(RefundRecordService.class);

    public RefundRecordService(RefundRecordMapper refundRecordMapper) {
        this.refundRecordMapper = refundRecordMapper;
    }

    public PageResult<RefundRecord> refundRecordList(Map<String, Object> params) {
        // 提取分页参数，确保非 null
        int page = params.containsKey("page") ? (int) params.get("page") : 1;
        int size = params.containsKey("size") ? (int) params.get("size") : 10;
        int offset = (page - 1) * size;

        // 提取搜索参数（允许 null）
        String refundNumber = params.containsKey("refundNumber") ? (String) params.get("refundNumber") : null;
        Integer userId = params.containsKey("userId") ? (Integer) params.get("userId") : null;
        Integer orderId = params.containsKey("orderId") ? (Integer) params.get("orderId") : null;
        String refundReason = params.containsKey("refundReason") ? (String) params.get("refundReason") : null;
        String processStatus = params.containsKey("processStatus") ? (String) params.get("processStatus") : null;

        // 日志调试（生产环境可移除）
        logger.info("分页查询退款记录参数: page={}, size={}, offset={}, refundNumber={}, userId={}, orderId={}, refundReason={}, processStatus={}",
                page, size, offset, refundNumber, userId, orderId, refundReason, processStatus);

        // 查询分页数据
        List<RefundRecord> list = refundRecordMapper.selectByPage(offset, size, refundNumber, userId, orderId, refundReason, processStatus);

        // 查询总条数
        long total = refundRecordMapper.selectCount(refundNumber, userId, orderId, refundReason, processStatus);

        return new PageResult<>(list, total);
    }

    public boolean createRefundRecord(RefundRecord refundRecord) {
        return refundRecordMapper.insertSelective(refundRecord) > 0;
    }

    public boolean updateRefundRecord(RefundRecord refundRecord) {
        return refundRecordMapper.updateByPrimaryKeySelective(refundRecord) > 0;
    }

    public boolean deleteRefundRecord(Integer id) {
        return refundRecordMapper.deleteByPrimaryKey(id) > 0;
    }

    public boolean batchDeleteRefundRecord(List<Integer> ids) {
        return refundRecordMapper.batchDeleteByIds(ids) > 0;
    }

    public RefundRecord getRefundRecordById(Integer id) {
        return refundRecordMapper.selectByPrimaryKey(id);
    }

    public boolean updateRefundRecordStatus(Integer id, String processStatus) {
        RefundRecord refundRecord = new RefundRecord();
        refundRecord.setId(id);
        refundRecord.setProcessStatus(processStatus);
        return refundRecordMapper.updateByPrimaryKeySelective(refundRecord) > 0;
    }
}